package com.graduation.service;

import com.graduation.dao.CountryDao;
import com.graduation.dao.GenderDao;
import com.graduation.dao.ShopDao;
import com.graduation.entity.Country;
import com.graduation.entity.Gender;
import com.graduation.entity.Shop;
import com.graduation.exception.ShopIsExitsException;
import com.graduation.util.DateUtil;
import com.graduation.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/*
 *@Author:君上
 *@Date 2022/3/18 9:27
 *@Content:
 */

@Service
public class ShopServiceImpl implements ShopService {

    @Resource
    private ShopDao shopDao;
    @Resource
    private CountryDao countryDao;
    @Resource
    private GenderDao genderDao;

    @Transactional
    @Override
    public Integer openStore(Shop shop) {
        Integer result = 0;
        Integer countrySize = 0;
        Integer genderSize = 0;
        //首先判断是否在关联表当中是否存在
        countrySize = countryDao.findCountryById(shop.getCountry());
        genderSize = genderDao.findGenderIsExists(shop.getGender());
        //如果关联的数据存在
        if ((countrySize) != 0 && (genderSize) != 0) {
            shop.setRegdate(DateUtil.getCurrentDate());
            result = shopDao.openStore(shop);
        } else {
            Country country = null;
            Gender gender = null;
            if (countrySize == 0) {
                //先插入城市信息
                country = new Country();
                country.setId(shop.getCountry());
                country.setCountry(StringUtil.findCountryById(shop.getCountry()));
                //插入关联表
                result = countryDao.insertCountryItem(country);
            }
            if (genderSize == 0) {
                //插入性别信息
                gender = new Gender();
                gender.setId(Integer.valueOf(shop.getGender()));
                gender.setGender(StringUtil.findGenderById(shop.getGender()));
                result = genderDao.insertGenderInfo(gender);
            }
            //大于1
            if (result >= 1) {
                shop.setState("营业");
                shop.setRegdate(DateUtil.getCurrentDate());
                shopDao.openStore(shop);
            }
        }
        return result;
    }

    @Override
    public Shop findShopMsg(String email) {
        Shop shopMsg = shopDao.findShopMsg(email);
        return shopMsg;
    }

    @Override
    public Integer insertEmailToShop(Shop shop) throws ShopIsExitsException {
        return shopDao.insertEmailToShop(shop);
    }

    @Override
    public Integer queryShopId(String email) {
        return shopDao.queryShopId(email);
    }


}
